<?php

/**
* 货品控制器
*/
class GoodsListController extends AuthController{
	
    /*货品列表
	 * 
     **/
    public function index(){
    	if(IS_POST){
    	    $model=K('GoodsList');
			if(!$model->addGoodsList()) $this->error($model->error);
			$this->success('添加成功',NULL,0.2);
    	}
        $gid=Q("get.gid",0,'intval');
	//**获得该商品选择的属性
		$attr=M()->join("__goods_attr__ as ga join __type_attr__ as ta on ga.tp_aid=ta.tp_aid")
				 ->where("goods_gid={$gid} and tp_aclass=1")->all();	
		//整理上面获得的数组，按照属性id分类		
		$temp=array();
		foreach ($attr as $k => $a) {
			$temp[$a['tp_aid']][]=$a;
		}
		$this->assign('attr',$temp);
	//**获得该商品下的货品
		$listData=M('goods_list')->where("goods_gid={$gid}")->all();
		//获得该商品的规格属性，获得的数组键名是属性id(货品表组合字段保存的东西),键值是属性值
		$attrValue=M('goods_attr')->where("goods_gid={$gid}")->getField('gaid,attr_value',true);
		foreach ($listData as $k => $v) {
			$listData[$k]['combine']=explode(',', $v['combine']);
			foreach ($listData[$k]['combine'] as $key => $value) {
				//拆分成数组后每个元素的值就是属性id(gaid),作为键名在商品属性数组中取值
				$listData[$k]['combine'][$key]=$attrValue[$value];
			}
		}
		$this->assign('listData',$listData);
        $this->display();
    }
	
	/*编辑
	 **/
	public function editGoodsList(){
		if(IS_POST){
		    $model=K('GoodsList');
			$model->editGoodsList();
			$this->success('修改成功',NULL,0.3);
		}
		
	}
	
	
	/*删除一种货品
	 **/
	public function del(){
	    $glid=Q('get.glid',0,'intval');
		$gid=Q('get.gid',0,'intval');
		M('goods')->dec('store',"gid={$gid}",Q('get.inventory',0,'intval'));
		M("goods_list")->where("glid={$glid}")->delete();
		$this->success('删除成功',NULL,0.5);
	}
	
	/*验证组合的货品规格是否重复
	 **/
	public function ajaxUnique(){
	    if(!IS_AJAX) return;
		$combine=Q('post.combine');
		$combine=trim($combine,',');
		$gid=Q('get.gid',0,'intval');
		$data=M('goods_list')->where("goods_gid={$gid}")->getField('combine',true);
		if(in_array($combine, $data)){
			echo 1;die;
		}else{
			echo 0;die;
		}
	}
}
    
    
    
?>